package cn.itcast.controller;

import cn.itcast.domain.Good;
import cn.itcast.domain.User;
import cn.itcast.service.GoodService;
import cn.itcast.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class SysController {
    @Autowired
    private UserService userService;

    @Autowired
    private GoodService goodService;


    @RequestMapping(value = {"/", "index"})
    public String index(HttpSession session, Model m) {
        Object obj = session.getAttribute("user");
        if (obj != null) {
            m.addAttribute("user", (User) obj);
        }
        List<Good> goods = goodService.findAll();
        m.addAttribute("goods",goods);
        return "index";
    }

    @RequestMapping(value = {"/toLogin"})
    public String login() {
        return "login";
    }

    @RequestMapping(value = {"/loginOut"})
    public String loginOut(HttpSession session) {
        Object obj = session.getAttribute("user");
        session.removeAttribute("user");
        return "login";
    }

}
